package com.ruoyi.web.controller.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.BusinessCard;
import com.ruoyi.system.domain.DUserBcd;
import com.ruoyi.system.service.IBusinessCardService;
import com.ruoyi.system.service.IDUserBcdService;
import com.ruoyi.web.domian.sqlReturn;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/bcd_api")
public class sqlBcdController extends BaseController {
    @Autowired
    private IDUserBcdService IDUserBcdService;
    @Autowired
    private IBusinessCardService IBusinessCardService;

    @PostMapping("/user_bcd_bangding")
    @ResponseBody
    public sqlReturn user_bcd_bangding(String bcd_id,String user_id){
        try {
            System.out.println(bcd_id+"\t"+user_id);
            DUserBcd dUserBcd = new DUserBcd();
            dUserBcd.setUserId(Long.valueOf(user_id));
            dUserBcd.setBcId(Long.valueOf(bcd_id));
//            检查是否绑定过关系
            List<DUserBcd> list=IDUserBcdService.selectDUserBcdList(dUserBcd);
            BusinessCard BusinessCard=new BusinessCard();
            BusinessCard.setUserId(Long.valueOf(user_id));
            BusinessCard.setBcId(Long.valueOf(bcd_id));
            List<BusinessCard> bcd_lsit=IBusinessCardService.selectBusinessCardList(BusinessCard);

            if(list.size()==0 && bcd_lsit.size()==0){
//                绑定关系
                dUserBcd.setInsDate(new Date());
                IDUserBcdService.insertDUserBcd(dUserBcd);
            }
            return new sqlReturn(null,"成功",200);

        }catch (AuthenticationException e){
            return new sqlReturn(null,"网络异常",400);

        }

    }



    @PostMapping("/getbct_all")
    @ResponseBody
    public sqlReturn getbct_all(String bcId){
        sqlReturn set=new sqlReturn(null, "成功", 200 );
        try {
            BusinessCard bct=IBusinessCardService.selectBusinessCardByBcId(Long.valueOf(bcId));
            set.setData(bct);
            return  set;
        }catch (AuthenticationException e){
            set.setStatus(400);
            set.setMsg("失败");
            return  set;
        }
    }

//    新增数据
    @PostMapping("/add_bcd")
    @ResponseBody
    public sqlReturn add_bcd(BusinessCard businessCard){
        System.out.println(businessCard);
        sqlReturn set=new sqlReturn(null, "成功", 200 );
        try {
            int i = IBusinessCardService.insertBusinessCard(businessCard);
            System.out.println(i+"--------------");
            return  set;
        }catch (AuthenticationException e){
            set.setStatus(400);
            set.setMsg("失败");
            return  set;
        }
    }


//    更新数据
    @PostMapping("/update_bcd")
    @ResponseBody
    public sqlReturn update_bcd(BusinessCard businessCard){
        System.out.println(businessCard);

        sqlReturn set=new sqlReturn(null, "成功", 200 );
        try {
            int i = IBusinessCardService.updateBusinessCard(businessCard);
            System.out.println(i+"--------------");
            return  set;
        }catch (AuthenticationException e){
            set.setStatus(400);
            set.setMsg("失败");
            return  set;
        }
    }

}
